﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Telerik.Windows.Controls;
using NHibernate.Cfg;
using Broker.View;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Company;
using BrokerBase.Lib.Client;
using System.Threading;
using Broker.View.ViewModel;

namespace Broker.Mgr
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private Thread loadDataThread;

        public App()
        {
            StyleManager.ApplicationTheme = new MetroTheme();
            this.Startup += new StartupEventHandler(App_Startup);
        }


        void App_Startup(object sender, StartupEventArgs e)
        {
            try
            {
                log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());

                AppGlobal.Log = log;

                Configuration cfg;

                cfg = new Configuration();

                cfg.Configure();

                AppGlobal.HbmSessionFactory = cfg.BuildSessionFactory();

                NHibernate.Context.ThreadStaticSessionContext.Bind(AppGlobal.HbmSessionFactory.OpenSession());

                //AppGlobal.GetAppPath();

                AppGlobal.ClientCfg = ClientConfiguration.Load(AppGlobal.GetAppPath()+ @"\" + ClientConfiguration.USER_FILE_NAME);
                
                SetConfig();

                AsyncLoadData();

                Broker.View.LibGlobal.HbmSessionFactory = AppGlobal.HbmSessionFactory;

                Broker.View.LibGlobal.BaseDataType = AppGlobal.BaseDataType;

                Broker.View.LibGlobal.AppType = AppType.Manager;

                Broker.View.LibGlobal.Log = AppGlobal.Log;

                BrokerBase.Lib.BaseLibGlobal.Log = AppGlobal.Log;

                Broker.View.LibGlobal.GetCurrentCompany = () =>
                {
                    return AppGlobal.CurrentCompany;
                };

                Broker.View.LibGlobal.GetClientCfg = () =>
                {
                    return AppGlobal.ClientCfg;
                };

                Broker.View.LibGlobal.GetAppPath = () =>
                {
                    return AppGlobal.GetAppPath();
                };

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (AppGlobal.Log != null)
                {
                    AppGlobal.Log.Error(ex.Message);
                }

                
                
            }


        }


        public void AsyncLoadData()
        {
            try
            {
                loadDataThread = new Thread(LoadData);
                loadDataThread.Start();

            }
            catch (Exception ex)
            {
                AppGlobal.Log.Error("异步读取数据是发生错误", ex);
            }
        }

        public void LoadData()
        {
            LibGlobal.ProductNames = ProductNameModel.LoadAllBasic(0);

            LibGlobal.PersonNames = PersonNameModel.LoadAllPerson();
        }

        private void SetConfig()
        {

            if (AppGlobal.ClientCfg.DefaultCompany == "[全部]")
            {
                ICompany cmp = new Company();
                cmp.Name = "[全部]";
                cmp.Id = 0;

                AppGlobal.CurrentCompany = cmp;
            }
            else
            {

                IBaseService<Company> svr = new BaseService<Company>(AppGlobal.HbmSessionFactory.GetCurrentSession());

                ICompany cmp = svr.GetByHQL(string.Format("from Company where Name ='{0}'", AppGlobal.ClientCfg.DefaultCompany));

                AppGlobal.CurrentCompany = cmp;
            }
        }

    }
}
